package com.gitee.feizns.explore.data_structure.array.easy;

/**
 * 665. 非递减数列
 * @author feizns
 * @since 2020/3/24
 */
public class CheckPossibility {

    public static void main(String[] args) {
        System.out.println(checkPossibility2(new int[]{3, 4, 2, 3}));
    }

//    public static boolean checkPossibility(int[] nums) {
////        int ret = 0;
////        int min = Integer.MIN_VALUE;
////        for (int i = 0; i < nums.length - 1; i++) {
////            if ( nums[i] > nums[i + 1] ) {
////                ret++;
////                if ( nums[i + 1] > min ) {
////                    min = nums[i + 1];
////                    nums[i + 1] = nums[i];
////                }
////            }
////            if ( ret == 2 )
////                return false;
////        }
////        return true;
//    }

    public static boolean checkPossibility2(int[] nums) {
        int count = 0;
        int temp = Integer.MIN_VALUE;
        for(int i = 0; i < nums.length - 1; i++){
            if( nums[i] > nums[i + 1] ){
                if( count++ > 0 )
                    return false;
                if( nums[i + 1] < temp )
                    nums[i + 1] = nums[i];
                else
                    nums[i] = temp;
            }
            temp = nums[i];
        }
        return true;
    }

}
